package com.ruoyi.job.service.impl;

import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.job.domain.BzDataEntity;
import com.ruoyi.job.mapper.IBzDataMapper;
import com.ruoyi.job.service.IBzDataService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class BzDataServiceImpl extends ServiceImpl<IBzDataMapper, BzDataEntity> implements IBzDataService {
    @Override
    public JSONArray getDataByIp(String ip) {
        BzDataEntity bzDataEntity = this.lambdaQuery().eq(BzDataEntity::getIp, ip).orderByDesc(BzDataEntity::getCreateTime).last("limit 1").one();
        if (bzDataEntity != null) {
            return bzDataEntity.getJsonData();
        }
        return null;
    }

    @Override
    public BzDataEntity getFirst(LocalDateTime yesterdayStart, LocalDateTime yesterdayEnd, String ip) {
        LambdaQueryWrapper<BzDataEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(BzDataEntity::getCreateTime, yesterdayStart)
                .le(BzDataEntity::getCreateTime, yesterdayEnd)
                .eq(BzDataEntity::getIp, ip)
                .orderByAsc(BzDataEntity::getCreateTime)
                .last("limit 1");
         return getOne(queryWrapper);
    }

    @Override
    public BzDataEntity getEnd(LocalDateTime yesterdayStart, LocalDateTime yesterdayEnd, String ip) {
        LambdaQueryWrapper<BzDataEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(BzDataEntity::getCreateTime, yesterdayStart)
                .le(BzDataEntity::getCreateTime, yesterdayEnd)
                .eq(BzDataEntity::getIp, ip)
                .orderByDesc(BzDataEntity::getCreateTime)
                .last("limit 1");
        return getOne(queryWrapper);
    }
}
